package edu.gatech.doi.spamframework.gui;

// JAXP's classes for DOM I/O
import javax.xml.parsers.*;

// GUI classes
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//Standard Java Classes
import java.io.*;

/**
 * This is just a Tester class for the XTree class.  I didn't bother giving
 * it full JavaDoc documentation because you won't want to reuse this class
 * in your own development.
 *
 * @author Kyle Gabhart
 * @version 1.0
 */

public class XTreeTester extends JFrame
{
  // This is the XTree object which displays the XML in a JTree
  private XTree               xTree;
  // This JScrollPane is the container for the JTree
  private JScrollPane         jScroll;
  // This Listener allows the frame's close button to work properly
  private WindowListener      winClosing;

  // These two constants set the width and height of the frame
  private static final int FRAME_WIDTH = 400;
  private static final int FRAME_HEIGHT = 300;

  /*
   * This constructor builds a frame containing a JScrollPane which in turn contains an XTree
   * object based on the XML string passed into the constructor
   */
  public XTreeTester( String title, String xml ) throws ParserConfigurationException
  {
      // This builds the JFrame portion of the object
      super( title );

      Toolkit			toolkit;
      Dimension			dim, minimumSize;
      int			screenHeight, screenWidth;

      // Initialize basic layout properties
      setBackground( Color.lightGray );
      getContentPane().setLayout( new BorderLayout() );

      // Set the frame's display to be WIDTH x HEIGHT in the middle of the screen
      toolkit = Toolkit.getDefaultToolkit();
      dim = toolkit.getScreenSize();
      screenHeight = dim.height;
      screenWidth = dim.width;
      setBounds( (screenWidth-FRAME_WIDTH)/2, (screenHeight-FRAME_HEIGHT)/2, FRAME_WIDTH, FRAME_HEIGHT );

      // Build the XTree object
      xTree = new XTree( xml );

      // Wrap the XTree in a JScroll so that we can scroll it in the JFrame.
      jScroll = new JScrollPane();
      jScroll.getViewport().add( xTree );

      // Add the scroll pane to the frame
      getContentPane().add( jScroll, BorderLayout.CENTER );

      //Put the final touches to the JFrame object
      validate();
      setVisible(true);

      // Add a WindowListener so that we can close the window
      winClosing = new WindowAdapter()
      {
         public void windowClosing(WindowEvent e)
         {
            exit();
         }
      };
      addWindowListener(winClosing);
  } //end XTreeTester()

  // Program execution begins here.  An XML file (*.xml) must be passed into the method
  public static void main( String[] args )
  {
      String                    fileName = "";
      BufferedReader            reader;
      String                    line;
      StringBuffer              xmlText;
      XTreeTester               xTreeTester;

      // Build a Document object based on the specified XML file
      try
      {
        if( args.length > 0 )
      	{
            fileName = args[0];

            if ( fileName.substring( fileName.indexOf( '.' ) ).equals( ".xml" ) )
            {
              reader = new BufferedReader( new FileReader( fileName ) );
              xmlText = new StringBuffer();

              while ( ( line = reader.readLine() ) != null )
              {
                xmlText.append( line );
              } //end while ( ( line = reader.readLine() ) != null )

              // The file will have to be re-read when the Document object is parsed
              reader.close();

              // Construct the GUI components and pass a reference to the XML root node
              xTreeTester = new XTreeTester( "XTree Tester", xmlText.toString() );
            }
            else
            {
              help();
            } //end if ( fileName.substring( fileName.indexOf( '.' ) ).equals( ".xml" ) )
        }
        else
        {
          help();
        } //end if( args.length > 0 )
      }
      catch( FileNotFoundException fnfEx )
      {
         System.out.println( fileName + " was not found." );
         exit();
      }
      catch( Exception ex )
      {
         ex.printStackTrace();
         exit();
      }// end try/catch
   }// end main()

   // A common source of operating instructions
   private static void help()
   {
      System.out.println( "\nUsage: java XTreeTester filename.xml" );
      System.exit(0);
   } //end help()

   // A common point of exit
   private static void exit()
   {
      System.out.println( "\nThank you for using the XTreeTester" );
      System.exit(0);
   } //end exit()
} //end XTreeTester
